
"""

TODO: Get a basic level system working
      Clean up and generalise code structure a bit
      
"""

# General imports
import os, sys
import random
import math
import pygame
from utils_for_pygame import *
import Vector2 as vec
from Vector2 import Vector

# Game imports
from game import *

if __name__ == '__main__':
    print 'Initialising engine...'
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("Bound 'n' Pound")
    pygame.mouse.set_visible(0)
    
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((0, 0, 0))
    
    screen.blit(background, (0, 0))
    pygame.display.flip()
    
    area = screen.get_rect()
    clock = pygame.time.Clock()
    
    print "Initialising game..."
    game = Game()
    
    # Key bindings (a bit crude)
    key_down_map = {K_LEFT: game.players[0].run_left, \
                    K_RIGHT: game.players[0].run_right, \
                    ord('a'): game.players[1].run_left, \
                    ord('d'): game.players[1].run_right}
    key_up_map = {K_LEFT: game.players[0].stop_left, \
                  K_RIGHT: game.players[0].stop_right, \
                  ord('a'): game.players[1].stop_left, \
                  ord('d'): game.players[1].stop_right}
    # Note: Jump commands use continuous input to allow continuous jumping
    key_query_map = {K_UP: (game.players[0].jump_start, \
                                game.players[0].jump_end), \
                     ord('w'): (game.players[1].jump_start, \
                                    game.players[1].jump_end)}
    
    # Main loop
    # ---------
    running = True
    game.tick_count = 0
    print 'Beginning main loop...'
    while running:
        clock.tick(game.tickrate)
        game.tick_count += 1
        
        # Input events
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    running = False
                for (key,action) in key_down_map.items():
                    if event.key == key:
                        action()
                        break
            elif event.type == KEYUP:
                for (key,action) in key_up_map.items():
                    if event.key == key:
                        action()
                        break
            elif event.type == MOUSEBUTTONUP:
                pass
            elif event.type == MOUSEMOTION:
                pass
        
        # Continuous input
        key_down = pygame.key.get_pressed()
        for (key,action) in key_query_map.items():
            if key_down[key]:
                if(action[0]): action[0]()
            else:
                if(action[1]): action[1]()
        
        # This is where all the action occurs
        game.update()
        
        pygame.display.set_caption("Bound 'n' Pound" + \
                                       ", P1: " + str(game.players[0].score) + \
                                       ", P2: " + str(game.players[1].score) + \
                                       ", FPS: " + str(clock.get_fps()))
        
        # Clear the whole screen
        screen.blit(background, (0, 0))
        #level.draw()
        game.draw(screen)
        # TODO: Draw other components here...
        pygame.display.flip()

    print 'Shutting down...'
    pygame.quit()

    print 'Done.'
